package com.tang.lts.core.autoconfigure;

import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.Set;

/**
 * @author Tang
 * @classname RelaxedNames
 * @description [ 宽松的名称 ]
 * @date 2022/4/12 22:09
 */
public final class RelaxedNames implements Iterable<String> {

    private final String name;

    private final Set<String> values = new LinkedHashSet<>();

    public RelaxedNames(String name) {
        this.name = name;
        initialize(RelaxedNames.this.name,this.values);
    }

    private void initialize(String name, Set<String> values) {
        for (Variation variation : Variation.values()) {
            for (Manipulation manipulation : Manipulation.values()) {
                String result = name;
                result = manipulation.apply(result);
                result = variation.apply(result);
                values.add(result);
            }
        }
    }

    /**
     * 名称变化
     */
    enum Variation {
        NONE(){
            @Override
            public String apply(String value) {
                return value;
            }
        },
        lowercase(){
            @Override
            public String apply(String value) {
                return value.toLowerCase();
            }
        },
        uppercase(){
            @Override
            public String apply(String value) {
                return value.toUpperCase();
            }
        }
        ;

        public abstract String apply(String value);
    }

    /**
     * 名称操作
     */
    enum Manipulation {
        NONE {
            @Override
            public String apply(String value) {
                return value;
            }
        },
        HYPHEN_TO_UNDERSCORE {
            @Override
            public String apply(String value) {
                return value.replace("-", "_");
            }
        }
        ;
        public abstract String apply(String value);
    }

    @Override
    public Iterator<String> iterator() {
        return values.iterator();
    }

    public static RelaxedNames forCameCase(String name) {
        return new RelaxedNames(name);
    }

}